

import open3d as o3d
import numpy as np

# 获取点云数据
demo_crop_data = o3d.data.DemoCropPointCloud()
pcd = o3d.io.read_point_cloud(demo_crop_data.point_cloud_path)

# 根据json文件的范围裁剪点云
vol = o3d.visualization.read_selection_polygon_volume(demo_crop_data.cropped_json_path)

# 裁剪出的椅子点云
chair = vol.crop_point_cloud(pcd)

# 计算原点云到目标点云的距离
# pcd.compute_point_cloud_distance(target) 计算源点云（pcd）中每个点到目标点云（target）的最近距离
dists = pcd.compute_point_cloud_distance(chair)
# 返回NumPy数组
dists = np.asarray(dists)

# 找到距离大于0.01的点的索引
ind = np.where(dists > 0.01)[0]
# 反向筛选的话，返回的就是椅子

# 按索引筛选出这些点
# pcd.select_by_index(indices) 根据索引列表（indices） 从点云中筛选点，返回新的点云
pcd_without_chair = pcd.select_by_index(ind)
o3d.visualization.draw_geometries([pcd_without_chair],
                                  zoom=0.3412,
                                  front=[0.4257, -0.2125, -0.8795],
                                  lookat=[2.6172, 2.0475, 1.532],
                                  up=[-0.0694, -0.9768, 0.2024])